package com.eatwhat.mapper;

import com.eatwhat.entity.UserChoice;
import org.apache.ibatis.annotations.*;
import java.time.LocalDate;
import java.util.List;

@Mapper
public interface UserChoiceMapper {
    
    @Insert("INSERT INTO user_choice(user_id, food_id, choice_date) " +
            "VALUES(#{userId}, #{foodId}, #{choiceDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserChoice userChoice);
    
    @Select("SELECT uc.*, f.name as food_name FROM user_choice uc " +
            "LEFT JOIN food f ON uc.food_id = f.id " +
            "WHERE uc.user_id = #{userId} ORDER BY uc.create_time DESC LIMIT 10")
    List<UserChoice> findRecentByUserId(String userId);
    
    @Select("SELECT uc.*, f.name as food_name FROM user_choice uc " +
            "LEFT JOIN food f ON uc.food_id = f.id " +
            "WHERE uc.user_id = #{userId} AND uc.choice_date = #{date}")
    List<UserChoice> findByUserIdAndDate(@Param("userId") String userId, @Param("date") LocalDate date);
    
    @Select("SELECT f.id, f.name, COUNT(*) as choice_count FROM user_choice uc " +
            "LEFT JOIN food f ON uc.food_id = f.id " +
            "WHERE uc.choice_date >= #{startDate} AND uc.choice_date <= #{endDate} " +
            "GROUP BY f.id, f.name ORDER BY choice_count DESC LIMIT 10")
    List<Object> getPopularFoods(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
}